package org.jinghouyu.caculator.engine.node;

import org.jinghouyu.caculator.engine.Expression;
import org.jinghouyu.caculator.engine.ExpressionContext;
import org.jinghouyu.caculator.engine.expression.ValueExpression;

public class ExpressionNode implements ParsableNode {

	private String expression;
	
	public ExpressionNode(String expression) {
		this.expression = expression;
	}
	
	public Expression parse(ExpressionContext context) {  //排序，先
		if(isValue()) return new ValueExpression(Double.parseDouble(expression));
		return NodeParser.parse(expression, context);
	}
	
	// TODO 改成逐字判断，使用异常，效率低
	private boolean isValue() {
		try {
			Double.parseDouble(expression);
			return true;
		} catch(NumberFormatException e) {
			return false;
		}
	}
}
